﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crawler.Model;
using Husb.Util;
using Crawler.BusinessActions;
using System.Transactions;
using Husb.Framework;

namespace WinFromsClient
{
    public partial class TaskForm : Form
    {
        private TaskInfo task;
        private DataViewMode mode;
        public TaskForm(TaskInfo task, DataViewMode mode)
        {
            InitializeComponent();

            WinUtil.PopulateListBox(taskTypeComboBox, ProjectConstants.TaskType, false);
            WinUtil.PopulateListBox(encodingComboBox, ProjectConstants.Encoding, false);
            this.taskTypeComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.taskInfoBindingSource, "TaskType", true));
            this.encodingComboBox.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.taskInfoBindingSource, "Encoding", true));

            this.taskInfoBindingSource.DataSource = task;
            this.task = task;
            this.taskItemBindingSource.DataSource = task.TaskItems;

            TaskItemInfo taskItem = this.taskItemBindingSource.Current as TaskItemInfo;

            if (taskItem != null)
            {
                taskItem.CaptureRules = new BindingListView<CaptureRuleInfo>( CaptureRule.GetByMasterId(taskItem.Id));
                this.captureRuleBindingSource.DataSource = taskItem.CaptureRules;
            }

            this.mode = mode;
        }

        private void btnAddUrl_Click(object sender, EventArgs e)
        {
            //task.EndEdit();
            OpenTaskItemForm(DataViewMode.Insert);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.taskInfoBindingSource.EndEdit();
            // 开始保存

            if (mode == DataViewMode.Insert)
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    Task.Insert(task);
                    foreach (TaskItemInfo taskItem in task.TaskItems)
                    {
                        TaskItem.Insert(taskItem);
                        foreach (CaptureRuleInfo rule in taskItem.CaptureRules)
                        {
                            CaptureRule.Insert(rule);
                        }
                    }
                    ts.Complete();
                }
            }
            else
            {
                Task.Update(task);
            }
        }

        private void taskItemBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            TaskItemInfo item = this.taskItemBindingSource.Current as TaskItemInfo;
            if (item != null)
            {
                this.captureRuleBindingSource.DataSource = item.CaptureRules;
            }
        }

        private void btnEditUrl_Click(object sender, EventArgs e)
        {
            OpenTaskItemForm(DataViewMode.Edit);
        }

        private void OpenTaskItemForm( DataViewMode mode)
        {
            if (task.IsValid)
            {
                TaskItemInfo taskItem = null;

                if (mode == DataViewMode.Edit)
                {
                    taskItem = this.taskItemBindingSource.Current as TaskItemInfo;
                }
                else
                {
                    taskItem = new TaskItemInfo();
                    taskItem.Id = Guid.NewGuid();
                    taskItem.Task = task;
                    taskItem.TaskId = task.Id;
                }


                UrlForm form = new UrlForm(taskItem, mode);
                if (DialogResult.OK == form.ShowDialog())
                {
                    if (mode == DataViewMode.Insert)
                    {
                        task.TaskItems.Add(taskItem);
                    }
                    else
                    {
                        TaskItem.Update(taskItem);
                    }
                }
            }
            else
            {
                errorProvider1.SetError(nameTextBox, task.ValidationMessage);
            }
        }

        private void btnAddRule_Click(object sender, EventArgs e)
        {
            TaskItemInfo taskItem = this.taskItemBindingSource.Current as TaskItemInfo;
            if (taskItem != null)
            {
                OpenCaptureRuleForm(taskItem, DataViewMode.Insert);
            }
        }

        private void btnEditRule_Click(object sender, EventArgs e)
        {
           
            TaskItemInfo taskItem = this.taskItemBindingSource.Current as TaskItemInfo;
            if (taskItem != null)
            {
                OpenCaptureRuleForm(taskItem, DataViewMode.Edit);
            }
        }

        private void btnDeleteRule_Click(object sender, EventArgs e)
        {

        }

        private void btnDeleteUrl_Click(object sender, EventArgs e)
        {

        }

        private void OpenCaptureRuleForm(TaskItemInfo taskItem, DataViewMode mode)
        {
            if (taskItem.IsValid)
            {
                CaptureRuleInfo cr;
                if (mode == DataViewMode.Edit)
                {
                    cr = this.captureRuleBindingSource.Current as CaptureRuleInfo;
                    cr.TaskItem = this.taskItemBindingSource.Current as TaskItemInfo;
                }
                else
                {
                    cr = new CaptureRuleInfo();
                    cr.Id = Guid.NewGuid();
                    cr.TaskItem = taskItem;
                    cr.Url = taskItem.Url;
                    cr.TaskItemId = taskItem.Id;
                }
                CaptureRuleForm form = new CaptureRuleForm(cr);
                if (DialogResult.OK == form.ShowDialog())
                {
                    if (mode == DataViewMode.Edit)
                    {
                        CaptureRule.Update(cr);
                    }
                    else
                    {
                        taskItem.CaptureRules.Add(cr);
                        if (this.mode == DataViewMode.Edit)
                        {
                            CaptureRule.Insert(cr);
                        }
                    }
                }
            }
            else
            {
                //errorProvider1.SetError(urlTextBox, taskItem.ValidationMessage);
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            //TaskItemInfo taskItem = this.taskItemBindingSource.Current as TaskItemInfo;
            //CopyUrlForm form = new CopyUrlForm(taskItem);
            //form.ShowDialog();
        }
    }
}
